import urllib, re, sys, zipfile, os

baseurl = 'http://www.mpg123.de/download/win32/'

print 'Searching latest mpg123 library...'
cont = urllib.urlopen(baseurl).read()
maxrevision = 0
maxrevisionfname = None
for fname, revision in re.findall('href="(mpg123-r(\d+)-win\d\d.zip)"', cont):
    if int(revision) > int(maxrevision):
        maxrevision = revision
        maxrevisionfname = fname

if not maxrevisionfname:
    print 'Not found'
    sys.exit(0)

print 'Found', maxrevisionfname

print 'Downloading...'
liburl = baseurl + maxrevisionfname
urllib.urlretrieve(liburl, maxrevisionfname)

print 'Unzipping...'
rootDir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mpg123')
zipfile.ZipFile(maxrevisionfname).extractall(rootDir)

print 'Done.'
